package com.bnlr.lrparking.appui.publish.openenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.ApplyEnterEvent;
import com.bnlr.lrparking.model.BaseArrayBean;
import com.bnlr.lrparking.model.VerifyCodeM;
import com.bnlr.lrparking.util.CountDownTimerUtils;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 提交申请入驻资料
 */
public class ApplyEnterActivity extends BaseActivity {

    private Unbinder unbinder;
    @BindView(R.id.tv_nav_title)
    TextView textView;
    @BindView(R.id.apply_company)
    EditText etCompany;
    @BindView(R.id.apply_name)
    EditText etName;
    @BindView(R.id.apply_phone)
    EditText etPhone;
    @BindView(R.id.apply_verify)
    EditText etVerify;
    @BindView(R.id.tv_verifica)
    TextView tvVerify;
    private CountDownTimerUtils mCountDownTimerUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_enter);
        unbinder = ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        textView.setText("填写资料");
        etPhone.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if (RegexUtils.isMobileExact(s.toString())) {
                    tvVerify.setEnabled(true);
                } else {
                    tvVerify.setEnabled(false);
                }
            }
        });
        tvVerify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendYzm();
            }
        });
    }
    @OnClick({R.id.open_enter_apply})
    public void openOnClick(View view) {
        switch (view.getId()) {
            case R.id.open_enter_apply:
                String company = etCompany.getText().toString();
                String name = etName.getText().toString();
                String phone = etPhone.getText().toString();
                String code = etVerify.getText().toString();
                if (TextUtils.isEmpty(company)
                        || TextUtils.isEmpty(name)
                        || TextUtils.isEmpty(phone)
                        || TextUtils.isEmpty(code)) {
                    ToastUtils.showShort("请填写完信息再提交");
                    return;
                } else if (!RegexUtils.isMobileExact(phone)){
                    ToastUtils.showShort("手机号填写不正确");
                    return;
                }else {
                    applyR();
                }
                break;
            default:
                break;
        }
    }

    private void applyR() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.Developer, LrConstant.POST);
            mRequest.add("action", "ApplyToDeveloper");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("name", etCompany.getText().toString());
            mRequest.add("linkMan", etName.getText().toString());
            mRequest.add("phone", etPhone.getText().toString());
            mRequest.add("code", etVerify.getText().toString());
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<BaseArrayBean>(this, true, BaseArrayBean.class) {
                        @Override
                        public void doWork(BaseArrayBean data, String code) {

                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            if (TextUtils.equals("1", code)) {
                                startActivity(new Intent(ApplyEnterActivity.this,ApplyEnterSuccessActivity.class));
                            } else {
                                try {
                                    ToastUtils.showShort(obj.getString("msg"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }, false);
        } catch (Exception e) {
            Log.e("6666666",e.getMessage());
        }
    }
    private void SendYzm() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "sendValidateMCD");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("phone", etPhone.getText().toString());
            mRequest.add("flag", "0");
            // 添加到请求队列
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<VerifyCodeM>(this, true, VerifyCodeM.class) {
                        @Override
                        public void doWork(VerifyCodeM data, String code) {
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {//{"msgcode":"1","msg":"成功","data":{"code":"06641"}}
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    setVerifica(tvVerify);
                                } else {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void setVerifica(TextView tvVerify) {
        mCountDownTimerUtils = new CountDownTimerUtils(tvVerify, 60000, 1000);
        mCountDownTimerUtils.start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onApplyEnterEvent(ApplyEnterEvent event) {
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        if (mCountDownTimerUtils != null) {
            mCountDownTimerUtils.cancel();
        }
    }
}
